package com.xiaoqiudong.forum.controller;

import com.xiaoqiudong.forum.common.AppResult;
import com.xiaoqiudong.forum.common.ResultCode;
import com.xiaoqiudong.forum.exception.ApplicationException;
import com.xiaoqiudong.forum.model.Board;
import com.xiaoqiudong.forum.service.IBoardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Descroption
 * User:dell
 * Date:2025-08-25
 * Time:11:12
 */
@Slf4j
@RestController
@Tag(name = "板块接口")
@RequestMapping("/board")
public class BoardController {
    //获取配置中的板块数，如果没有默认位9
    @Value("${bit-forum.index.board-num:9}")
    private Integer boardNum;
    @Resource
    private IBoardService boardService;

    /**
     * 查询板块列表
     * @return
     */
    @GetMapping("/toList")
    @Operation(description = "获取首页板块列表")
    public AppResult<List<Board>> toList(){
        log.info("板块个数："+ boardNum);
        List<Board> boards = boardService.selectByNum(boardNum);
        if (boards == null){
            boards = new ArrayList<>();
        }
        return AppResult.success(boards);
    }

    /**
     * 根据板块id 查询板块信息
     * @param boardId
     * @return
     */
    @GetMapping("getById")
    @Operation(description = "获取板块消息")
    public AppResult<Board> getById(@Parameter(name = "id",description = "板块id") @RequestParam("id") @Nonnull Long boardId){
        Board board = boardService.selectById(boardId);
        if (board == null || board.getDeleteState() == 1){
            //板块不存在或者板块已经删除
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //返回结果
        return AppResult.success(board);
    }
}
